package net.loyintean.pearlshell.validation.rule;

import java.util.Hashtable;
import java.util.Map;

import net.loyintean.pearlshell.validation.IValidRule;
import net.loyintean.pearlshell.validation.IValidRuleConfig;
import net.loyintean.pearlshell.validation.config.ValidConfig;

class SingletonValidFactory implements IValidFactory {

	private static final Map<String, IValidRule> ruleMap = new Hashtable<String, IValidRule>();

	static final SingletonValidFactory FACTORY = new SingletonValidFactory();

	private SingletonValidFactory() {
		ruleMap.put(ValidConfig.OBJ_NOT_NULL.ruleName(),
				new ObjNotNullValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_ALL_CHINESE.ruleName(),
				new StringAllChineseValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_IS_FLOAT.ruleName(),
				new StringIsFloatValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_IS_INT.ruleName(),
				new StringIsIntValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_MAX_LENGTH.ruleName(),
				new StringMaxLengthValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_MIN_LENGTH.ruleName(),
				new StringMinLengthValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_NOT_BLANK.ruleName(),
				new StringNotBlankValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_WITHOUT_CHINESE.ruleName(),
				new StringWithoutChineseValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_IS_PHONENUM.ruleName(),
				new StringPhoneNumValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_IS_IDNUM.ruleName(),
				new StringIdentifyCardValidRuleImpl());

		ruleMap.put(ValidConfig.STRING_IS_EMAIL.ruleName(),
				new StringIsEmailValidRuleImpl());
	}

	@Override
	public IValidRule buildRule(IValidRuleConfig config) {

		assert config != null;

		IValidRule rule = ruleMap.get(config.ruleName());

		assert rule != null;

		return rule;
	}

	@Override
	public void registRule(String ruleName, IValidRule rule) {
		assert ruleName != null;
		assert rule != null;

		ruleMap.put(ruleName, rule);
	}
}
